package com.itao.spel;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * @author sjt
 * @Date 2024/10/9 9:55
 */
public class Methods {

    @SuppressWarnings("all")
    public static void main(String[] args) throws NoSuchMethodException {
        ExpressionParser parser = new SpelExpressionParser();
        // string literal, evaluates to "bc"
        String bc = parser.parseExpression("'abc'.substring(1, 3)").getValue(String.class);
        System.out.println(bc); // bc

//        EvaluationContext context = new StandardEvaluationContext(new Society());
//        // evaluates to true
//        boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')").getValue(context, Boolean.class);

        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("isMember", Society1.class.getMethod("isMember", String.class));
        // evaluates to true
        boolean isMember = parser.parseExpression("#isMember('Mihajlo Pupin')").getValue(context, Boolean.class);
        System.out.println(isMember);
    }

    static class Society1 {
        public static boolean isMember(String name) {
            return name.equals("Mihajlo Pupin");
        }
    }
}
